package com.mc.developmentkit.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qamaster.android.R;
import com.mc.developmentkit.bean.UserInfo;
import com.mc.developmentkit.utils.DbUtils;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.views.TitleManger;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
/**
 * Created by LeBron on 2017/1/3.
 */

public class UserCenterActivity extends Activity implements View.OnClickListener{

    private ImageView user_Icon;
    private TextView user_Account;
    private TextView user_Money;
    private TextView user_Look;
    private Button user_Cancellation;//注销
    private RelativeLayout re_CZ, re_AQ, re_CallMe;
    private View view_Line;//第三方登录需要设置隐藏

    private DbManager db;
    private UserInfo userInfo;

    private RelativeLayout fankui;
    private String systemIcon="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1483526849540&di=490d8522699f129f4360e3ddc0c05184&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D3773084700%2C3737944561%26fm%3D214%26gp%3D0.jpg";

    public static Activity context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usercenter);
        TitleManger insetance = TitleManger.getInsetance();
        insetance.setContext(this);
        insetance.setName("个人中心");
        initView();
        initData();
    }

    private void initView() {
        context=this;
        user_Icon = (ImageView) findViewById(R.id.user_logo);
        user_Account = (TextView) findViewById(R.id.user_Account);
        user_Money = (TextView) findViewById(R.id.user_money);
        user_Look = (TextView) findViewById(R.id.user_lookptb);
        user_Cancellation = (Button) findViewById(R.id.user_zhuxiao);
        re_CZ = (RelativeLayout) findViewById(R.id.re_cz);
        fankui = (RelativeLayout) findViewById(R.id.fankui);
        re_AQ = (RelativeLayout) findViewById(R.id.re_aq);
        re_CallMe = (RelativeLayout) findViewById(R.id.re_we);
        view_Line = findViewById(R.id.sinh5);

        user_Icon.setOnClickListener(this);
        user_Look.setOnClickListener(this);
        re_CZ.setOnClickListener(this);
        re_AQ.setOnClickListener(this);
        fankui.setOnClickListener(this);
        re_CallMe.setOnClickListener(this);
        user_Cancellation.setOnClickListener(this);
    }
    private void initData() {
        db = DbUtils.getDB();
        try {
            userInfo = db.selector(UserInfo.class).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        if (userInfo != null) {
            user_Account.setText(userInfo.nickname);
            if (userInfo.login == 3 || userInfo.login == 4) {         //第三方登录设置修改密码选项不可见
                MCUtils.disPlay(user_Icon, userInfo.icon, true);
                re_AQ.setVisibility(View.GONE);
                view_Line.setVisibility(View.GONE);
                return;
            }else{
                MCUtils.disPlay(user_Icon, systemIcon, true);
            }
        } else {
            startActivity(new Intent(this,LoginActivity.class));
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.user_zhuxiao) {
            try {
                db.delete(UserInfo.class);
            } catch (DbException e) {
                e.printStackTrace();
            }
           startActivity(new Intent(this,LoginActivity.class));
            finish();
        } else if (i == R.id.re_cz) {
            startActivity(new Intent(this,RechargeActivity.class));
        } else if (i == R.id.re_aq) {
           startActivity(new Intent(this,AccountSecurityActivity.class));
        } else if (i == R.id.re_we) {
           startActivity(new Intent(this,ContactUsActivity.class));
        } else if (i == R.id.fankui) {
           startActivity(new Intent(this,FeedBackActivity.class));
        }
    }
}
